package org.example.agent.task;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import com.alibaba.fastjson.JSON;
import org.example.constant.CommonConstant;
import org.example.dto.TaskStatus;
import org.example.enums.EDeployTaskStep;
import org.example.enums.EJobType;
import org.example.enums.ETaskErrCode;
import org.example.model.CopyFileParam;
import org.example.task.HttpTask;
import org.example.task.ITask;
import org.example.util.ThreadUtil;
import org.example.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 文件拷贝任务
 */
public class FileCopyTask extends ParentTask implements ITask {

    private static final Logger LOGGER = LoggerFactory.getLogger(FileCopyTask.class);

    private CopyFileParam copyParam;

    private Result<TaskStatus> taskResult;

    public FileCopyTask(){

    }

    public FileCopyTask(String params){
        this.copyParam = JSON.parseObject(HexUtil.decodeHexStr(params, CharsetUtil.CHARSET_UTF_8), CopyFileParam.class);
    }

    @Override
    public Result<TaskStatus> execute() {
        LOGGER.info("FileCopyTask 。。。start ,params={}", copyParam);
        Result<TaskStatus> result = Result.ok();
        ThreadUtil.threadWaitSeconds(10);
        this.taskResult = result;
        LOGGER.info("FileCopyTask 。。。end");
        return result;
    }

    @Override
    public Result<String> reportStatus() {
        this.taskResult = handleResult(this.taskResult, ETaskErrCode.FILECOPY);
        TaskStatus taskStatus = taskResult.getResult();
        taskStatus.setJobId(copyParam.getJobId());
        taskStatus.setTaskId(copyParam.getTaskId());
        taskStatus.setJobType(EJobType.DEPLOY);
        taskStatus.setTaskStep(EDeployTaskStep.COPY.getStepNum());
        this.taskResult.setResult(taskStatus);
        HttpTask httpTask = new HttpTask(CommonConstant.MASTER_HOST_URL+"admincenter/plan/statereport",JSON.toJSONString(this.taskResult));
        Result<String> result = httpTask.execute();
        printErrMsg(result, EJobType.DEPLOY, EDeployTaskStep.COPY);
        LOGGER.info("FileCopyTask reportStatus taskResult={}",this.taskResult);
        return result;
    }



}
